﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Core.Foundation.Standards;
using System.Text;


namespace Core.Foundation.WebParts.CrossSiteListView
          
{
    /// <summary>
    /// CrossSiteListViewUserControl : UserControl
    /// </summary>
    public partial class CrossSiteListViewUserControl : UserControl
    {
       
        #region Properties
        /// <summary>
        /// SelectedSite
        /// </summary>
        public string SelectedSite
        {
            get;
            set;
        }
        /// <summary>
        /// SelectedSubSite
        /// </summary>
        public string SelectedSubSite
        {
            get;
            set;
        }
        /// <summary>
        /// SelectedList
        /// </summary>
        public string SelectedList
        {
            get;
            set;
        }
        /// <summary>
        /// SelectedView
        /// </summary>
        public string SelectedView
        {
            get;
            set;
        }
        /// <summary>
        /// AutoRefreshInterval
        /// </summary>
        public string AutoRefreshInterval
        {
            get;
            set;
            
        }

        /// <summary>
        /// Filter
        /// </summary>
        public string Filter
        {
            get;
            set;

        }
        
        #endregion



         

        /// <summary>
        /// Page_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
           
            try
            {
                               
                SetTimerForAutoRepresh();
                                
                lbl.Text = DateTime.Now.ToString();

                if (!string.IsNullOrEmpty(SelectedSite) && !string.IsNullOrEmpty(SelectedSubSite)
                    && !string.IsNullOrEmpty(SelectedList) && !string.IsNullOrEmpty(SelectedView))
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(SelectedSite))
                        {
                            SPWeb web = site.OpenWeb(SelectedSubSite);

                            Guid webSafe = CoreHelper.AllowUnSafeUpdates(ref web);

                            SPList list = web.Lists[SelectedList];
                            SPView view = list.Views[SelectedView];

                            bool alreadyTabularView = view.TabularView;

                            //if the view is a TabularView then we turn it off
                            if (alreadyTabularView)
                            {
                                view.TabularView = false;
                                view.Update();
                            }

                            ListQuery.List = list;
                            ListQuery.DisableFilter = false;

                            SPQuery query = new SPQuery(ListQuery.List.Views[SelectedView]);

                            if (!string.IsNullOrEmpty(Filter))
                            {
                                query.Query = Filter;
                            }

                            ListQuery.Query = query;
                           
                            //if the source view was tabular when we started we switch it back
                            if (alreadyTabularView)
                            {
                                view.TabularView = true;
                                view.Update();
                            }

                            CoreHelper.AllowUnSafeUpdates(ref web, webSafe);

                            web = null;
                        }
                    });
                }

            }
            catch (Exception ex)
            {
                Logging.LogError(ex, FoundationConstants.webpartsLog);                
            }
           

        }
                 

        private void SetTimerForAutoRepresh()
        {
            int NewValue;
            try
            {
                if(string.IsNullOrEmpty(AutoRefreshInterval))
                {
                    // Default value.30 sec
                    NewValue=30000;
                }
                else
                {
                    NewValue=Convert.ToInt32(AutoRefreshInterval)* 1000;
                }
            }
            catch
            {
                // Default value.30 sec
                NewValue = 30000;
            }
            //Timer1.Interval = 1;
            Timer1.Interval=NewValue;

        }
    }
}
